<?php
namespace app\site\controller;

use app\common\model\SiteModel;
use app\common\model\UserModel;
use think\Request;

class BaseController extends \think\Controller
{
    private $uid;
    private $siteID;
    /**
     * 初始化
     * BaseController constructor.
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $des = is_login($this->getUtype());
        if ($des === false) {
            $this->error('请登录后再操作！',url('Public/Login'));
        }else{
            $this->uid = $des['uid'];
            $this->siteID = $des['site_id'];
            $this->assign('uid',$this->uid);
            $this->assign('site_id',$this->siteID);
        }
    }

    /**
     * 获取用户类型
     * @return string
     */
    public function getUtype()
    {
        return 'site';
    }

    /**
     * 获取用户UID
     * @return bool|mixed
     */
    public function getUid()
    {
        return $this->uid;
    }

    /**
     * 获取站点ID
     * @return int
     */
    public function getSiteId()
    {
        return $this->siteID;
    }

    /**
     * 获取站点资料
     * @param string $field
     * @return array|false|mixed|\PDOStatement|string|\think\Model
     */
    public function getSiteInfo($field='all'){
        $siteModel = new SiteModel();
        $info = $siteModel->where('site_id',$this->getSiteId())->find();
        if($field!='all'){
            return $info[$field];
        }
        return $info;
    }
    /**
     * 获取会员组ID
     */
    public function getGroupId(){
        $userModel = new UserModel();
        $groupId = $userModel->where('uid',$this->getUid())->value('group_id');
        return $groupId?$groupId:0;
    }

}